#include<iostream>
using namespace std;
#include<vector>
#include<queue>
#include<unordered_map>
class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        int arr[2001] = { 0 };
        unordered_map<int, vector<int>> find;
        for (auto& e : prerequisites) {
            ++arr[e[0]];
            find[e[1]].emplace_back(e[0]);
        }
        vector<int> ans;
        queue<int> que;
        for (int i = 0; i < numCourses; ++i)
            if (arr[i] == 0) {
                que.push(i);
                ans.push_back(i);
            }
        while (que.size()) {
            int now = que.front();
            que.pop();
            for (auto& e : find[now]) {
                --arr[e];
                if (arr[e] == 0) {
                    que.push(e);
                    ans.push_back(e);
                }
            }
        }
        if (ans.size() != numCourses)
            return {};
        return ans;
    }
};